package com.shitou.shitouproduct.service.impl;

import com.shitou.shitoucommons.exception.RRException;
import com.shitou.shitoucommons.utils.RespBean;
import com.shitou.shitouproduct.entity.BrandEntity;
import com.shitou.shitouproduct.entity.CategoryEntity;
import com.shitou.shitouproduct.service.BrandService;
import com.shitou.shitouproduct.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.Query;

import com.shitou.shitouproduct.dao.CategoryBrandRelationDao;
import com.shitou.shitouproduct.entity.CategoryBrandRelationEntity;
import com.shitou.shitouproduct.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Resource
    private CategoryService categoryService;

    @Resource
    private BrandService brandService;

    /**
     * 创建相关表
     * @param categoryBrandRelation
     * @return
     */
    @Override
    public RespBean mySave(CategoryBrandRelationEntity categoryBrandRelation) {

        CategoryBrandRelationEntity one = this.getOne(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", categoryBrandRelation.getBrandId())
                .eq("category_id", categoryBrandRelation.getCategoryId()));

        // 获取品牌
        BrandEntity brand = brandService.getOne(new QueryWrapper<BrandEntity>()
                .eq("brand_id", categoryBrandRelation.getBrandId())
                .eq("status", 1));
        if ( brand == null ){
            throw new RRException("获取品牌失败");
        }
        System.out.println(categoryBrandRelation);
        // 获取分类
        CategoryEntity category = categoryService.getOne(new QueryWrapper<CategoryEntity>()
                .eq("category_id", categoryBrandRelation.getCategoryId())
                .eq("status", 1));
        if ( category == null ){
            throw new RRException("获取分类失败");
        }

        // 更新
        if ( one != null ){
            one.setBrandName(brand.getName());
            one.setCategoryName(category.getName());
            one.setStatus(1);
            this.updateById(one);

        }else {
            // 插入
            categoryBrandRelation.setBrandName(brand.getName());
            categoryBrandRelation.setCategoryName(category.getName());
            categoryBrandRelation.setStatus(1);
            baseMapper.insert(categoryBrandRelation);
        }
        return RespBean.success("添加关联成功");
    }


    /**
     * 获取关联表，通过品牌id,,,admin后台用
     * @param brandId
     * @return
     */
    @Override
    public RespBean getRelationByBrandId(Long brandId) {
        List<CategoryBrandRelationEntity> brand_id = this.list(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id", brandId));

        return RespBean.success().put("data", brand_id);
    }


}
